package top.songxs.gmall.canel.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import top.songxs.gmall.common.constant.GmallCacheConstant;

import java.util.List;

@CanalEventListener
public class CanalServerListener {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @ListenPoint(schema = "gmall_product", table = "sku_info")
    public void updateMethod(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //获取更新前的数据
        List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();

        beforeColumnsList.stream().forEach(column -> {
            String name = column.getName();
            String value = column.getValue();
            System.out.println("列名" + name + "= 值：" + value);
        });
        //获取更新后的数据
        List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
        afterColumnsList.stream().forEach(column -> {
            String name = column.getName();
            String value = column.getValue();
            System.out.println("列名" + name + "= 值：" + value);
            if (name.equals("id")) {
                //删除redis中的缓存数据
                redisTemplate.delete(GmallCacheConstant.REDIS_SKU_INFO + column.getValue());
            }
        });


    }
}
